﻿#include "StdAfx.h"
#include <GdiPlus.h>

namespace ui
{
	bool ImageUtil::GetImageCLSID(const wchar_t* format, CLSID* pCLSID)
	{
		UINT num = 0;
		UINT size = 0;
		Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
		Gdiplus::GetImageEncodersSize(&num, &size);

		if (size == 0)
			return false;

		pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
		if (pImageCodecInfo == NULL)
			return false;
		GetImageEncoders(num, size, pImageCodecInfo);
		
		for (UINT i = 0; i < num; ++i)
		{
			if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
			{
				*pCLSID = pImageCodecInfo[i].Clsid;
				free(pImageCodecInfo);
				return true;
			}
		}
		free(pImageCodecInfo);

		return false;
	}
	//bool ImageUtil::SaveImage(Gdiplus::Image& image, const std::wstring& file_path, std::wstring mime_type, long quality_num/* = 95*/)
	//{
	//	CLSID clsid;
	//	if (!GetImageCLSID(mime_type.c_str(), &clsid))
	//		return false;

	//	Gdiplus::EncoderParameters encoder_paramters;
	//	encoder_paramters.Count = 1;
	//	encoder_paramters.Parameter[0].Guid = Gdiplus::EncoderQuality;
	//	encoder_paramters.Parameter[0].NumberOfValues = 1;
	//	encoder_paramters.Parameter[0].Type = Gdiplus::EncoderParameterValueTypeLong;
	//	encoder_paramters.Parameter[0].Value = (void*)&quality_num;
	//	Gdiplus::Status status = image.Save(file_path.c_str(), &clsid, &encoder_paramters);
	//	if (status != Gdiplus::Status::Ok)
	//	{
	//		int err = status;
	//		printf("SaveImage Error: %d\n", err);
	//	}
	//	return status == Gdiplus::Status::Ok;
	//}




} // namespace ui
